package com.lanou.horo.zhuizhui.entity.greendao;

import android.content.ContentValues;
import android.database.sqlite.SQLiteDatabase;

import com.lanou.horo.zhuizhui.utility.imageLoader.BaseApplication;

import java.util.List;

import de.greenrobot.dao.query.QueryBuilder;


/**
 * 帮助类
 **/
public class UserMsgDaoHelper {
    private static UserMsgDaoHelper helper;
//    private static com.lanou.horo.zhuizhui.entity.greendao.DaoSession daoSession;
    private static DaoSession daoSession;

    private UserMsgDaoHelper() {
    }

    public static UserMsgDaoHelper getInstance() {
        if (helper == null) {
            helper = new UserMsgDaoHelper();
            daoSession = BaseApplication.getDaoSession();
        }
        return helper;
    }

    // 保存数据
    public void saveUSERMSGList(List<User_Message> usermsg) {
        daoSession.getUser_MessageDao().insertInTx(usermsg);
    }

    //更新某个对象
    public void add(User_Message u) {
        daoSession.getUser_MessageDao().update(u);

    }

    // 查询数据
    public List<User_Message> queryDataAll() {
        return daoSession.getUser_MessageDao().loadAll();
    }

    // 根据用户名查询数据
    public List<User_Message> queryByName(String name) {
        QueryBuilder<User_Message> qb = daoSession.getUser_MessageDao().queryBuilder();
        qb.where(User_MessageDao.Properties.Name.eq(name));
        return qb.list();
    }

    // 插入数据
    public long insert(ContentValues values) {
        SQLiteDatabase sq = daoSession.getUser_MessageDao().getDatabase();
        String tablename = daoSession.getUser_MessageDao().getTablename();
        return sq.insert(tablename, null, values);
    }

    //添加一个对象
    public void addUser(User_Message u) {
//		SQLiteDatabase sq = daoSession.getUSERMSGDao().getDatabase();
//		String tablename = daoSession.getUSERMSGDao().getTablename();
        daoSession.getUser_MessageDao().insert(u);

    }

    //按name返回某一个对象
    public User_Message getUsermsgByName(String name) {
        QueryBuilder<User_Message> qb = daoSession.getUser_MessageDao().queryBuilder();
        qb.where(User_MessageDao.Properties.Name.eq(name));
        List<User_Message> list = qb.list();
        if (qb.list() != null && list.size() > 0) {
            return list.get(0);
        }
        return null;
    }

    //按name更新数据
    public User_Message updateByName(String name) {
        QueryBuilder<User_Message> qb = daoSession.getUser_MessageDao().queryBuilder();
        qb.where(User_MessageDao.Properties.Name.eq(name));
        return qb.list().get(0);
    }


    public void update(User_Message user_message) {
        daoSession.getUser_MessageDao().insertOrReplace(user_message);
    }
}
